import { getFoodList } from '../../apis/food.js'
import productData from '../../utils/productData.js'
// pages/food/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productType: productData,
    location: '北京',
    foodList: [],
    pageNum: 1,
    isShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow() {
    const cityName = wx.getStorageSync('cityName')
    if (cityName) {
      this.setData({
        location: cityName
      })
    }
    // 获取食物列表
    this.getFoodListData()
  },

  // 点击去单项的详情页面
  goProdctDetail(e) {
    const {id} = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/productDetail/index?itemId=' + id,
    })
  },
  // 跳转到商品详情
  toProductDetail(e) {
    wx.navigateTo({
      url: '/pages/productType/index?id=' + e.currentTarget.dataset.id,
    })
  },
  // 监听上拉触底事件
  /* onReachBottom: function () {
    this.setData({
      pageNum: this.data.pageNum ++,
    })

    this.getFoodListData()
  }, */

  async getFoodListData() {
    // 清空旧的数据
    this.setData({
      foodList: [] // 或者根据需要选择是否清空
    });
    const res = await getFoodList(this.data.location, this.data.pageNum);
    if (res.status === 200) {
      this.setData({
        foodList: this.data.foodList.concat(res.data.result)
      });
    } else if (res.status === 500) {
      this.setData({
        isShow: true
      });
    }
  }

})